﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace WebApplication8
{
    public partial class _Default : System.Web.UI.Page
    {
        struct nastavitve_temperature
        {
            public System.Xml.XmlNode nastavitev_node;
            public string nastavitev_label_name;
            public TableCell nastavitev_txtbox_min;
            public TableCell nastavitev_txtbox_max;

            public nastavitve_temperature(System.Xml.XmlNode _nastavitev_node, string _nastavitev_label_name, TableCell _nastavitev_txtbox_min, TableCell _nastavitev_txtbox_max)
            {
                nastavitev_node = _nastavitev_node;
                nastavitev_label_name = _nastavitev_label_name;
                nastavitev_txtbox_min = _nastavitev_txtbox_min;
                nastavitev_txtbox_max = _nastavitev_txtbox_max;
            }
        }

        List<nastavitve_temperature> temp_settings = new List<nastavitve_temperature>();
        System.Xml.XmlDocument setting = new System.Xml.XmlDocument();


        protected void Page_Load(object sender, EventArgs e)
        {
            temp_settings.Clear();
            Label3.Text = System.DateTime.Now.ToString();
            setting.Load(Request.PhysicalApplicationPath + "settings.xml");
            //Initialize your control. 
            System.Xml.XmlNode senzorika = setting.SelectSingleNode("/regulator/senzorji");
            System.Xml.XmlNode krmilje = setting.SelectSingleNode("/regulator/krmiljenje");
            Button shrani = new Button();

            foreach (System.Xml.XmlNode podrocja in senzorika.ChildNodes)
            {
                Table tabela = new Table();
                

                foreach (System.Xml.XmlNode senzorji in podrocja.ChildNodes)
                {
                    TableRow vrstica = new TableRow();
                    TableCell ime = new TableCell();
                    TableCell vrednost = new TableCell();
                    TableCell min = new TableCell();
                    TableCell max = new TableCell();

                    ime.Text = senzorji.Attributes.GetNamedItem("name").Value;
                    ime.Font.Bold = true;
                    ime.ToolTip = senzorji.InnerText;
                    try
                    {
                        string vsebina = System.IO.File.ReadAllText(senzorika.Attributes.GetNamedItem("prefix").Value + senzorji.InnerText + senzorika.Attributes.GetNamedItem("suffix").Value);
                        Match match = Regex.Match(vsebina, senzorika.Attributes.GetNamedItem("regex").Value);
                        string temperature = (match.Groups[senzorika.Attributes.GetNamedItem("group").Value].Value);
                        vrednost.Text = temperature.Insert(temperature.Length - Convert.ToInt32(senzorika.Attributes.GetNamedItem("divide").Value), ".") + " °C";
                    }
                    catch
                    {
                        vrednost.Text = "n/a";
                    }

                    vrstica.Cells.Add(ime);
                    vrstica.Cells.Add(vrednost);

                    TextBox min_tb = new TextBox();
                    TextBox max_tb = new TextBox();


                    min_tb.ToolTip = "minimalna vrednost";
                    max_tb.ToolTip = "maksimalna vrednost";
                    shrani.Text = "Shrani";
                    shrani.Click += new EventHandler(shrani_Click);

                    if (!User.Identity.IsAuthenticated)
                    {
                        min_tb.Enabled = false;
                        max_tb.Enabled = false;
                        shrani.Visible = false;
                    }

                    System.Xml.XmlNode min_node = senzorji.Attributes.GetNamedItem("min");
                    System.Xml.XmlNode max_node = senzorji.Attributes.GetNamedItem("max");

                    if (min_node != null)
                    {
                        min_tb.Text = min_node.Value;
                        min.Controls.Add(min_tb);
                        vrstica.Cells.Add(min);
                    }

                    if (max_node != null)
                    {
                        max_tb.Text = max_node.Value;
                        max.Controls.Add(max_tb);
                        vrstica.Cells.Add(max);
                    }

                    temp_settings.Add(new nastavitve_temperature(senzorji, ime.Text, min, max));
                    tabela.Rows.Add(vrstica);
                }
                Panel1.Controls.Add(tabela);
                Panel1.Controls.Add(new LiteralControl("<br />"));
            }
            Panel1.Controls.Add(shrani);

            Table tabela1 = new Table();

            foreach (System.Xml.XmlNode izhodi in krmilje.ChildNodes)
            {
                TableRow vrstica = new TableRow();
                TableCell ime = new TableCell();
                TableCell vrednost = new TableCell();

                ime.Text = izhodi.Attributes.GetNamedItem("name").Value;
                ime.Font.Bold = true;
                ime.ToolTip = izhodi.InnerText;
                try
                {
                    string vsebina = System.IO.File.ReadAllText(krmilje.Attributes.GetNamedItem("prefix").Value + izhodi.InnerText + krmilje.Attributes.GetNamedItem("suffix").Value);
                    if (vsebina.Contains("0"))
                        vrednost.Text = izhodi.Attributes.GetNamedItem("on0").Value;
                    else if (vsebina.Contains("1"))
                        vrednost.Text = izhodi.Attributes.GetNamedItem("on1").Value;
                    else
                        vrednost.Text = "n/a";
                }
                catch
                {
                    vrednost.Text = "n/a";
                }
                vrstica.Cells.Add(ime);
                vrstica.Cells.Add(vrednost);
                tabela1.Rows.Add(vrstica);
            }
            Panel2.Controls.Add(tabela1);
            Panel2.Controls.Add(new LiteralControl("<br />"));

            Image dnevno = new Image();
            dnevno.ImageUrl = "regulacija-dnevno.png";
            Image tedensko = new Image();
            tedensko.ImageUrl = "regulacija-tedensko.png";
            Image mesecno = new Image();
            mesecno.ImageUrl = "regulacija-mesecno.png";
            

            Panel3.Controls.Add(dnevno);
            Panel3.Controls.Add(tedensko);
            Panel3.Controls.Add(mesecno);

        }

        void shrani_Click(object sender, EventArgs e)
        {
            foreach(nastavitve_temperature temp_setting in temp_settings)
            {
                try
                {
                    if (temp_setting.nastavitev_txtbox_min.HasControls())
                    {
                        TextBox min = ((TextBox)temp_setting.nastavitev_txtbox_min.Controls[0]);
                        decimal min_flt = Convert.ToDecimal(min.Text);
                        min.Text = Math.Round(min_flt, 2).ToString();
                        temp_setting.nastavitev_node.Attributes.GetNamedItem("min").Value = min.Text;
                    }
                }
                catch { }
                try
                {
                    if (temp_setting.nastavitev_txtbox_max.HasControls())
                    {
                        TextBox max = ((TextBox)temp_setting.nastavitev_txtbox_max.Controls[0]);
                        decimal max_flt = Convert.ToDecimal(max.Text);
                        max.Text = Math.Round(max_flt, 2).ToString();
                        temp_setting.nastavitev_node.Attributes.GetNamedItem("max").Value = max.Text;
                    }
                }
                catch { }
                setting.Save(Request.PhysicalApplicationPath + "settings.xml");
            }
        }
    }
}
